package cn.lina.middleware.dynamic.thread.pool.sdk.trigger.job;



import cn.lina.middleware.dynamic.thread.pool.sdk.domain.IDynamicThreadPoolService;
import cn.lina.middleware.dynamic.thread.pool.sdk.domain.model.entity.ThreadPoolConfigEntity;
import cn.lina.middleware.dynamic.thread.pool.sdk.registry.IRegistry;
import com.alibaba.fastjson2.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;

/**
 * @author 24449
 * @date 2025/8/13
 * @description 线程池数据上报任务
 */
public class ThreadPoolDateReportJob{

    private final Logger logger = LoggerFactory.getLogger(ThreadPoolDateReportJob.class);

    private final IDynamicThreadPoolService dynamicThreadPoolService;

    private final IRegistry registry;

    public ThreadPoolDateReportJob(IDynamicThreadPoolService dynamicThreadPoolService, IRegistry registry) {
        this.dynamicThreadPoolService = dynamicThreadPoolService;
        this.registry = registry;
    }

    /**
     * 编写定时任务执行上报的动作
     */
    @Scheduled(cron = "0/20 * * * * ?")
    public void execReportThreadPoolList(){
        List<ThreadPoolConfigEntity> threadPoolConfigEntities = dynamicThreadPoolService.queryThreadPoolList();
        registry.reportThreadPool(threadPoolConfigEntities);
        logger.info("上报线程池{}", JSON.toJSON(threadPoolConfigEntities));

        for (ThreadPoolConfigEntity threadPoolConfigEntity : threadPoolConfigEntities) {
            registry.reportThreadPoolConfigParameter(threadPoolConfigEntity);
            logger.info("上报线程池配置{}", JSON.toJSON(threadPoolConfigEntity));
        }
    }
}
